import { app, ipcMain, BrowserWindow, shell, dialog,Menu,webContents  } from 'electron';
import { App } from '../mainWindow';
import { newwinApp} from '../createWin';
import autoUpdate from '../autoUpdate'; //自动更新服务
export default class WindowActionController {
    static init() {
        WindowActionController.handleWindowAction();
    }

    private static handleWindowAction() {
		// 窗口最小化事件
		ipcMain.handle('wx:windowStatus:minimize', (event) => {
		    App.getMainWindow().minimize();
		})
		
		ipcMain.handle('wx:windowStatus:update',(event)=>{
			const autoUpdaterService = new autoUpdate(); //自动更新实例化
			autoUpdaterService.init();//自动更新初始化启动更新服务
		})
		
		// 窗口最大化事件
		ipcMain.handle('wx:windowStatus:maximize', (event) => {
		    App.getMainWindow().maximize();
			App.getMainWindow().webContents.send('wx:windowStatus:changed', { status: 'maximize' });
		})
		
		// 窗口最大化还原事件
		ipcMain.handle('wx:windowStatus:unmaximize', (event) => {
		    App.getMainWindow().unmaximize();
			App.getMainWindow().webContents.send('wx:windowStatus:changed', { status: 'unmaximize' });
		})
		
		// 窗口关闭事件
		ipcMain.handle('wx:windowStatus:closeWindow', (event) => {
		    App.getMainWindow().close();
		})
		
		// 子窗口关闭事件
		ipcMain.handle('wx:windowStatus:closeChildWindow', (event) => {
		    const currentWindow = webContents.getFocusedWebContents();
		    const curruntwin = webContents.fromId(currentWindow.id);
		    curruntwin.close();
		})
		
		// 窗口置顶事件
		ipcMain.handle('wx:windowStatus:alwaysOnTop', (event,boolean) => {
		    App.getMainWindow().setAlwaysOnTop(boolean);
		})
		
		//下载事件
		ipcMain.handle('wx:windowStatus:download',(event,url) => {
		    App.getMainWindow().webContents.downloadURL(url)
		})
		
		// 创建新窗口
		ipcMain.handle('wx:windowStatus:createWin', (event,data) => {
		    newwinApp.initNewWindow(data)
		})
		
		// 窗口可拖拽
		ipcMain.handle('wx:windowStatus:windowdrag', (event,data) => {
			console.log(data)
		    App.dragMainwindow(data)
		})
		
		// 右键菜单事件
		ipcMain.handle('wx:windowStatus:showmenu', (event,data) => {
			const template = [
			    {
					label: '全屏',
					visible: data, // 初始可见
					click: () => { event.sender.send('fullScreen', { status: 'fullScreen' }) }
			    },
				{
					label: '退出全屏',
					visible: !data, // 初始可见
					click: () => { event.sender.send('fullScreen', { status: 'fullScreen' }) }
				},
			    { type: 'separator' },
			    {
					label: '录屏',
					click: () => { event.sender.send('recordScreen', { status: 'recordScreen' }) }
				},
				{ type: 'separator' },
				{
					label: '开发者工具',
					click: () => {App.showDevTools()}
				},
			  ]
			  const menu = Menu.buildFromTemplate(template)
			  menu.popup({ window: BrowserWindow.fromWebContents(event.sender) })
		})
    }
}
